import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toInteger from "lodash/toInteger";
import _startsWith from "lodash/startsWith";
import _set from "lodash/set";
import _min from "lodash/min";
import _max from "lodash/max";
import _isString from "lodash/isString";
import _isInteger from "lodash/isInteger";
import _isFunction from "lodash/isFunction";
import _get from "lodash/get";
import _forEach from "lodash/forEach";
import _cloneDeep from "lodash/cloneDeep";

/**
 * utils.js
 * @date 2018/10/24
 */
import React from 'react';
import moment from 'moment';
import { Button, DatePicker, Input, InputNumber, TimePicker } from 'antd';
import intl from "../../utils/intl";
import { getDateFormat, getTimeFormat } from "../../utils/utils";
import { DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT } from "../../utils/constants";
import { mapCustomizeBuilder } from "../../utils/customize/helpers";
import Switch from "../Switch";
import Checkbox from "../Checkbox";
import Lov from "../Lov";
import ValueList from "../ValueList";
import Upload from "../Upload";
import { contextPrefix } from "./config";
var TextArea = Input.TextArea; // 支持的 输入组件类型

var componentTypes = {
  Checkbox: Checkbox,
  DatePicker: DatePicker,
  TimePicker: TimePicker,
  Lov: Lov,
  InputNumber: InputNumber,
  Switch: Switch,
  TextArea: TextArea,
  Input: Input,
  ValueList: ValueList,
  Button: Button
};
/**
 * 后处理组件属性
 */

export var postPropProcesses = {
  ValueList: postValueListPropProcess,
  Lov: postLovPropProcess
};

function postValueListPropProcess(props, dataSource, field) {
  var postProps = props;
  postProps.textValue = getDisplayValue({
    field: field,
    dataSource: dataSource,
    componentProps: props
  });

  if (!postProps.textField) {
    postProps.textField = "".concat(field.fieldName, "Meaning");
  }

  return postProps;
}

function postLovPropProcess(props, dataSource, field) {
  var postProps = props;
  postProps.textValue = getDisplayValue({
    field: field,
    dataSource: dataSource,
    componentProps: props
  });

  if (!postProps.textField) {
    postProps.textField = "".concat(field.fieldName, "Meaning");
  }

  return postProps;
}

export function defaultPostPropProcess(props) {
  return props;
}
/**
 * 后处理组件属性
 */

/**
 * 处理组件属性
 */

export var prePropProcesses = {
  Checkbox: getCheckboxProps,
  DatePicker: getDatePickerProps,
  Lov: getLovProps,
  InputNumber: getInputNumberProps,
  Switch: getSwitchProps,
  TextArea: getTextAreaProps,
  Input: getInputProps,
  ValueList: getValueListProps
};
export function defaultPrePropProcess(props, context) {
  var dealProps = dealObjectProps(props, context);
  return dealProps;
}
/**
 * 获取 Checkbox 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */

function getCheckboxProps(props, context) {
  return defaultPrePropProcess(props, context);
}
/**
 * 获取 DatePicker 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getDatePickerProps(props, context) {
  var dealProps = dealObjectProps(props, context);
  var mergeStyle = dealProps.style || {};

  if (!mergeStyle.width) {
    mergeStyle.width = '100%';
  }

  return _objectSpread(_objectSpread({
    placeholder: '',
    format: getDateFormat()
  }, dealProps), {}, {
    style: mergeStyle
  });
}
/**
 * 获取 Lov 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getLovProps(props, context) {
  var dealProps = dealObjectProps(props, context);
  var mergeStyle = dealProps.style || {};

  if (!mergeStyle.width) {
    mergeStyle.width = '100%';
  }

  return _objectSpread(_objectSpread({}, dealProps), {}, {
    style: mergeStyle
  });
}
/**
 * 获取 InputNumber 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getInputNumberProps(props, context) {
  var dealProps = dealObjectProps(props, context);
  var mergeStyle = dealProps.style || {};

  if (!mergeStyle.width) {
    mergeStyle.width = '100%';
  }

  return _objectSpread(_objectSpread({}, dealProps), {}, {
    style: mergeStyle
  });
}
/**
 * 获取 Switch 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getSwitchProps(props, context) {
  return defaultPrePropProcess(props, context);
}
/**
 * 获取 TextArea 组件的属性
 * @param {object[]} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getTextAreaProps(props, context) {
  return defaultPrePropProcess(props, context);
}
/**
 * 获取 Input 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getInputProps(props, context) {
  return defaultPrePropProcess(props, context);
}
/**
 * 获取 ValueList 组件的属性
 * @param {object} props - 属性
 * @param {object} context - 外面传进来的 this
 */


function getValueListProps(props, context) {
  var dealProps = dealObjectProps(props, context);
  var mergeStyle = dealProps.style || {};

  if (!mergeStyle.width) {
    mergeStyle.width = '100%';
  }

  return _objectSpread(_objectSpread({}, dealProps), {}, {
    style: mergeStyle
  });
}
/**
 * 处理组件属性
 */

/**
 * colLayout - Col 的属性
 */


var colLayout = {
  1: {
    span: 24
  },
  2: {
    span: 12
  },
  3: {
    span: 8
  },
  4: {
    span: 6
  }
};
/**
 * getColLayout - 获取字段布局 包含对应的 style
 * @param {number} [col=3] 每行 列数
 * @param {number} colspan 字段宽度
 */

export function getColLayout() {
  var col = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
  var colspan = arguments.length > 1 ? arguments[1] : undefined;

  var colL = _cloneDeep(colLayout[col] || colLayout[3]);

  if (_isInteger(colspan) && colspan > 1) {
    colL.span = _min([24, colL.span * colspan]);
  }

  return colL;
}

function dateGetValueFromEventFunc(dateMoment) {
  return dateMoment && dateMoment.format(DEFAULT_DATE_FORMAT);
}

function timeGetValueFromEventFunc(timeMoment) {
  return timeMoment && timeMoment.format(DEFAULT_TIME_FORMAT);
}

function dateGetValuePropFunc(dateStr) {
  return {
    value: dateStr && moment(dateStr, DEFAULT_DATE_FORMAT)
  };
}

function timeGetValuePropFunc(dateStr) {
  return {
    value: dateStr && moment(dateStr, DEFAULT_TIME_FORMAT)
  };
}

export function getGetValuePropFunc(field) {
  switch (field.componentType) {
    case 'DatePicker':
      return dateGetValuePropFunc;

    case 'TimePicker':
      return timeGetValuePropFunc;

    default:
      return undefined;
  }
}
export function getGetValueFromEventFunc(componentType) {
  switch (componentType) {
    case 'DatePicker':
      return dateGetValueFromEventFunc;

    case 'TimePicker':
      return timeGetValueFromEventFunc;

    default:
      return undefined;
  }
}
/**
 * 从 dataSource 获取初始值
 * @param {object} field - 字段
 * @param {object} dataSource - 数据源
 * Checkbox Switch 默认返回 0
 */

export function getInitialValue(_ref) {
  var field = _ref.field,
      _ref$dataSource = _ref.dataSource,
      dataSource = _ref$dataSource === void 0 ? {} : _ref$dataSource;

  switch (field.componentType) {
    case 'Switch':
    case 'Checkbox':
      return dataSource[field.fieldName] || 0;

    default:
      return dataSource[field.fieldName];
  }
}
/**
 * 获取组件的类型
 * @param {object} field - 字段
 * 默认返回 Input
 */

export function getComponentType(field) {
  return componentTypes[field.componentType] || Input;
}
/**
 * 处理组件(字段)的属性,
 * @param {object} field - 字段
 * @param {string} field.componentType - 组件类型
 * @param {object} context - 页面 context
 */

export function preProcessComponentProps(_ref2) {
  var _ref2$field = _ref2.field,
      field = _ref2$field === void 0 ? {} : _ref2$field,
      context = _ref2.context;
  var prePropProcess = prePropProcesses[field.componentType];

  if (_isFunction(prePropProcess)) {
    return prePropProcess(field.props, context, field);
  }

  return defaultPrePropProcess(field.props, context, field);
}
export function postProcessComponentProps(_ref3) {
  var _ref3$field = _ref3.field,
      field = _ref3$field === void 0 ? {} : _ref3$field,
      dealProps = _ref3.dealProps,
      dataSource = _ref3.dataSource;
  var postPropProcess = postPropProcesses[field.componentType];

  if (_isFunction(postPropProcess)) {
    return postPropProcess(dealProps, dataSource, field);
  }

  return defaultPostPropProcess(dealProps, dataSource, field);
}
/**
 *
 * @param {{field: object, dataSource: object, componentProps: object}}
 * @param {object} field - 字段
 * @param {object} [dataSource={}] - 数据
 * @param {object} componentProps - 属性
 */

export function getDisplayValue(_ref4) {
  var field = _ref4.field,
      _ref4$dataSource = _ref4.dataSource,
      dataSource = _ref4$dataSource === void 0 ? {} : _ref4$dataSource,
      componentProps = _ref4.componentProps;
  var displayFieldName = componentProps.displayField;
  var format;
  var dateStr;

  switch (field.componentType) {
    case 'ValueList':
    case 'Lov':
      return dataSource[displayFieldName] || dataSource["".concat(field.fieldName, "Meaning")] || dataSource[field.fieldName];

    case 'DatePicker':
      format = getDateFormat();
      dateStr = dataSource[displayFieldName] || dataSource[field.fieldName];
      return dateStr && moment(dateStr, DEFAULT_DATE_FORMAT).format(format);

    case 'TimePicker':
      format = getTimeFormat();
      dateStr = dataSource[displayFieldName] || dataSource[field.fieldName];
      return dateStr && moment(dateStr, DEFAULT_DATE_FORMAT).format(format);

    case 'Checkbox':
    case 'Switch':
      return _toInteger(dataSource[displayFieldName] || dataSource[field.fieldName]) === 1 ? intl.get('cpaas.common.status.yes') : intl.get('cpaas.common.status.no');

    default:
      return dataSource[displayFieldName] || dataSource[field.fieldName];
  }
}
/**
 * @param {{field: object, dataSource: object, organizationId: number, disableStyle: 'value' | '', componentProps: object}}
 * @param {object} field - 字段
 * @param {object} dataSource - 数据
 * @param {number} organizationId - 租户id
 * @param {object} componentProps - 组件的属性
 */

export function renderDisabledField(_ref5) {
  var field = _ref5.field,
      dataSource = _ref5.dataSource,
      organizationId = _ref5.organizationId,
      _ref5$componentProps = _ref5.componentProps,
      componentProps = _ref5$componentProps === void 0 ? {} : _ref5$componentProps;

  switch (field.componentType) {
    case 'Upload':
      return /*#__PURE__*/React.createElement(Upload, Object.assign({}, componentProps, {
        disabled: true,
        viewOnly: true,
        value: getDisplayValue({
          field: field,
          dataSource: dataSource,
          componentProps: componentProps
        }),
        organizationId: organizationId
      }));

    default:
      return /*#__PURE__*/React.createElement("div", {
        className: "compose-field-value-disabled"
      }, getDisplayValue({
        field: field,
        dataSource: dataSource,
        componentProps: componentProps
      }));
  }
}
/**
 * getWidthFromWord - 通过字符串确定宽度
 * @param {String} word - 字符串
 * @param {Number} minWidth - 最小宽度
 * @param {Number} maxWidth - 最大宽度
 * @param {Number} [defaultWidth=100] - 默认宽度
 * @param {Number} [fontWidth=12] - 每个字符的宽度
 * @param {Number} [paddingWidth=36] - 补偿额外宽度
 */

export function getWidthFromWord(_ref6) {
  var word = _ref6.word,
      minWidth = _ref6.minWidth,
      maxWidth = _ref6.maxWidth,
      _ref6$defaultWidth = _ref6.defaultWidth,
      defaultWidth = _ref6$defaultWidth === void 0 ? 100 : _ref6$defaultWidth,
      _ref6$fontWidth = _ref6.fontWidth,
      fontWidth = _ref6$fontWidth === void 0 ? 12 : _ref6$fontWidth,
      _ref6$paddingWidth = _ref6.paddingWidth,
      paddingWidth = _ref6$paddingWidth === void 0 ? 36 : _ref6$paddingWidth;
  var ret = defaultWidth;

  if (_isString(word)) {
    ret = word.length * fontWidth;

    if (_min) {
      ret = _max([ret, minWidth]);
    }

    if (_max) {
      ret = _min([ret, maxWidth]);
    }

    ret += paddingWidth;
  }

  return ret;
}
/**
 * 将属性解构成 对应的
 * @param {object} config - 属性
 * @param {object} context - 上下文
 * @param {{[excludeKey: string]: true}} excludeProps - 要排除的属性
 */

export function dealObjectProps(config, context, excludeProps) {
  var dealProps = {};
  var skipProps = excludeProps || {};

  _forEach(config, function (v, k) {
    if (!skipProps[k]) {
      _set(dealProps, k, getContextValue(context, v));
    }
  });

  return dealProps;
}
/**
 * 处理 key
 * @param {object} props - 属性
 * @param {{[excludeKey: string]: true}} excludeProps - 要排除的属性
 */

export function setProps(props, excludeProps) {
  var hasSetProps = {};
  var skipProps = excludeProps || {};

  _forEach(props, function (v, k) {
    if (!skipProps[k]) {
      hasSetProps[k] = v;
    }
  });

  return hasSetProps;
}
/**
 * 如果是context属性 则返回 context属性上的内容
 * 否则返回 pathOrValue
 * @param {object} context
 * @param {string} pathOrValue
 */

export function getContextValue(context, pathOrValue) {
  if (_isString(pathOrValue) && _startsWith(pathOrValue, contextPrefix)) {
    return _get(context, pathOrValue.substr(5));
  }

  return pathOrValue;
}

var _mapCustomizeBuilder = mapCustomizeBuilder('cpfm', 'ui_dynamic_component'),
    customizeSet = _mapCustomizeBuilder.set,
    customizeGet = _mapCustomizeBuilder.get;

export { customizeSet as set, customizeGet as get };